package com.share.dymamic.controller;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;

@Controller
public class BasicController {
    protected transient Logger logger = LoggerFactory.getLogger(getClass());
   
    /** http相关参数 **/
	private static final String DEFAULT_CONTENT_TYPE_NAME = "content-type";
	private static final String DEFAULT_CONTENT_TYPE_VALUE = "application/json;charset=UTF-8";
    
    
    /**通用操作返回错误**/
    public static String RESULT_ERROR = "error";
    /**通用操作返回成功**/
    public static String RESULT_SUCCESS = "success";

    public static String RESULT_ADD_SUCCESS = "add_success";
    public static String RESULT_EDIT_SUCCESS = "edit_success";
    public static String RESULT_DELETE_SUCCESS = "delete_success";

    /**操作结果常量**/
    public static String OPE_RESULT = "ope_result";
    /**操作结果流水号**/
    public static String OPE_NUMBER = "ope_number";

    /**
	 * 处理输出结果
	 * 
	 * @param response
	 * @param bizResponse
	 */
	protected void handleResponse(HttpServletResponse response, Object result) {
		response.setHeader(DEFAULT_CONTENT_TYPE_NAME, DEFAULT_CONTENT_TYPE_VALUE);
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			if(result instanceof String){
				pw.print(result);
			}else{
				String data = JSON.toJSONString(result,true);
				pw.print(data);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (pw != null)
				pw.flush();
				pw.close();
		}
	}
}
